﻿Public Class frm_SearchReplaceWindow
    Dim frm As frm_TextEdit
    Dim SearchIndex As Integer
    Private Const MaxHeight As Integer = 332
    Private Const MinHeight As Integer = 147
    Private isLoadAscii As Boolean = False
    Private DataLoading As Boolean = False
#Region "构造函数"
    Public Sub New(ByVal sText As String)

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        TB_SearchValue.Text = sText
    End Sub
    Public Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。

    End Sub
#End Region
    Public Property FatherForm() As frm_TextEdit
        Get
            Return frm
        End Get
        Set(ByVal value As frm_TextEdit)
            frm = value
        End Set
    End Property
    Private Sub Btn_Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Cancel.Click
        Me.Close()
    End Sub

    Private Sub Btn_Search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Search.Click
        SearchSub()
    End Sub
    Private Sub SearchSub()
        Dim SearchValue As String = TranslateText(TB_SearchValue.Text)
        Dim SearchOption As New System.Windows.Forms.RichTextBoxFinds
        If CB_UpowerdLower.Checked Then
            SearchOption = RichTextBoxFinds.MatchCase
        Else
            SearchOption = RichTextBoxFinds.None
        End If
        With DirectCast(frm, frm_TextEdit)

            If SearchIndex < 0 Then SearchIndex = 0
            SearchIndex = .TextBox.Find(SearchValue, SearchIndex, .TextBox.TextLength, SearchOption)
            If SearchIndex > 0 Then
                .TextBox.SelectionStart = SearchIndex
                .TextBox.SelectionLength = SearchValue.Length
                SearchIndex = SearchIndex + SearchValue.Length
            Else
                MessageBox.Show(Me, "已搜索至文档结束！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        End With
    End Sub
    Private Sub LoadAsciiValue()
        If isLoadAscii Then
            Me.Height = MinHeight
            isLoadAscii = False
        Else
            Me.Height = MaxHeight
            isLoadAscii = True
        End If
        If DataLoading = False Then
            '加载数据
            Dim sCount As Integer = 0
            Dim lItem As ListViewItem = Nothing
            For i = 1 To 1785
                If sCount = 0 Then
                    lItem = LV_Ascii.Items.Add(i)
                    lItem.SubItems.Add(ChrW(i))
                    sCount += 1
                ElseIf sCount = 1 Then
                    If lItem IsNot Nothing Then
                        lItem.SubItems.Add(i)
                        lItem.SubItems.Add(ChrW(i))
                    End If
                    sCount += 1
                ElseIf sCount = 2 Then
                    If lItem IsNot Nothing Then
                        lItem.SubItems.Add(i)
                        lItem.SubItems.Add(ChrW(i))
                    End If
                    sCount = 0
                End If
            Next
        End If
    End Sub

    Private Sub frm_SearchReplaceWindow_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.Height = MinHeight
    End Sub

    Private Sub Label3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label3.Click
        LoadAsciiValue()
    End Sub

    Private Sub Btn_Replace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Replace.Click
        Dim SearchValue As String = TranslateText(TB_SearchValue.Text)
        Dim ReplaceValue As String = TranslateText(TB_ReplaceValue.Text)
        Dim Result As String = ""
        Dim tmp As String = frm.TextBox.SelectedText
        Dim SearchOption As New System.Windows.Forms.RichTextBoxFinds

        With DirectCast(frm, frm_TextEdit)
            If CB_UpowerdLower.Checked Then
                If tmp = SearchValue Then
                    Result = frm.TextBox.Text.Substring(0, frm.TextBox.SelectionStart) & ReplaceValue & frm.TextBox.Text.Substring(frm.TextBox.SelectionStart + frm.TextBox.SelectedText.Length)
                    frm.TextBox.Text = Result
                    Return
                End If
                SearchOption = RichTextBoxFinds.MatchCase
            Else
                If tmp.ToLower = SearchValue.ToLower Then
                    Result = frm.TextBox.Text.Substring(0, frm.TextBox.SelectionStart) & ReplaceValue & frm.TextBox.Text.Substring(frm.TextBox.SelectionStart + frm.TextBox.SelectedText.Length)
                    frm.TextBox.Text = Result
                    Return
                End If
                SearchOption = RichTextBoxFinds.None
            End If
            If SearchIndex < 0 Or SearchIndex > .TextBox.TextLength Then SearchIndex = 0
            SearchIndex = .TextBox.Find(SearchValue, SearchIndex, .TextBox.TextLength, SearchOption)
            If SearchIndex > 0 Then
                .TextBox.SelectionStart = SearchIndex
                .TextBox.SelectionLength = SearchValue.Length
                SearchIndex = SearchIndex + SearchValue.Length
                Result = frm.TextBox.Text.Substring(0, frm.TextBox.SelectionStart) & ReplaceValue & frm.TextBox.Text.Substring(frm.TextBox.SelectionStart + frm.TextBox.SelectedText.Length)
                frm.TextBox.Text = Result
            Else
                MessageBox.Show(Me, "已搜索至文档结束！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        End With
        '  If Not String.IsNullOrEmpty(Result) Then frm.TextBox.Text = Result
    End Sub

    Private Sub Btn_ReplaceAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ReplaceAll.Click
        Try
            Dim SearchValue As String = TranslateText(TB_SearchValue.Text)
            Dim ReplaceValue As String = TranslateText(TB_ReplaceValue.Text)
            Dim Result As String = frm.TextBox.Text
            Dim Rcount As Long, Rindex As Long = 0
            Dim SearchOption As New System.Windows.Forms.RichTextBoxFinds
            With DirectCast(frm, frm_TextEdit)
                If CB_UpowerdLower.Checked Then
                    SearchOption = RichTextBoxFinds.MatchCase
                Else
                    SearchOption = RichTextBoxFinds.None
                End If
                If SearchIndex < 0 Or SearchIndex >= .TextBox.TextLength Then SearchIndex = 0
                Do Until SearchIndex < 0 Or SearchIndex >= Result.Length
                    SearchIndex = Search(Result, SearchIndex, SearchValue)
                    If SearchIndex >= 0 Then
                        Result = Result.Substring(0, SearchIndex) & ReplaceValue & Result.Substring(SearchIndex + SearchValue.Length)
                        SearchIndex = SearchIndex + SearchValue.Length
                        Rcount += 1
                    Else
                        Exit Do
                    End If
                Loop
                .TextBox.Text = Result
                If Rcount > 0 Then MessageBox.Show(Me, "成功替换" & Rcount & "处文本！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End With
        Catch ex As Exception
            MessageBox.Show(Me, ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
    Public Function Search(ByVal OldText As String, ByVal StartIndex As Integer, ByVal SearchValue As String) As Long
        If StartIndex < 0 Or StartIndex >= OldText.Length Then SearchIndex = -1
        Dim TmpS As String, sV As String
        Dim Result As Long
        If CB_UpowerdLower.Checked Then
            TmpS = OldText.Substring(StartIndex)
            sV = SearchValue
        Else
            TmpS = OldText.ToLower.Substring(StartIndex)
            sV = SearchValue.ToLower
        End If
        Result = TmpS.IndexOf(sV)
        If Result >= 0 Then
            Return Result + StartIndex
        Else
            Return -1
        End If
    End Function
End Class